package com.jiutong.meipengdai.ui.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiutong.meipengdai.R;
import com.jiutong.meipengdai.base.BaseActivity;
import com.jiutong.meipengdai.common.Constants;
import com.jiutong.meipengdai.entity.AccountSettingRespond;
import com.jiutong.meipengdai.http.model.interfaces.CheckLoginInterface;
import com.jiutong.meipengdai.http.model.requests.MyAccountRequest;
import com.jiutong.meipengdai.http.subscribers.ProgressSubscriber;
import com.jiutong.meipengdai.http.subscribers.SubscriberOnNextListener;
import com.jiutong.meipengdai.utils.SpConfig;
import com.jiutong.meipengdai.views.dialog.BaseDialog;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by suxi on 2017/6/1.
 */

@CheckLoginInterface
public class AccountSettingActivity extends BaseActivity {
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_phone)
    TextView tvPhone;
    @Bind(R.id.rl_phone_num)
    RelativeLayout rlPhoneNum;
    @Bind(R.id.tv_email)
    TextView tvEmail;
    @Bind(R.id.rl_email)
    RelativeLayout rlEmail;
    @Bind(R.id.rl_password)
    RelativeLayout rlPassword;
    @Bind(R.id.tv_turn_back)
    TextView tvTurnBack;
    @Bind(R.id.rl_back)
    RelativeLayout rl_back;

    BaseDialog dialog;
    private String phone;
    private String email;

    private MyAccountRequest mRequest;
    private SubscriberOnNextListener onNextListener;
    private HashMap<String, Object> fieldsMap;
    private ProgressSubscriber progressSubscriber;

    @Override
    public int getLayoutResouceId() {
        return R.layout.activity_account_setting;
    }

    @Override
    protected void initDate(Bundle savedInstanceState) {
        super.initDate(savedInstanceState);

    }

    @Override
    protected void onNewIntent(Intent intent) {

        Bundle bundle = intent.getExtras();
        if(bundle == null)
            return;
        if(!TextUtils.isEmpty(bundle.getString("phoneNum"))){
            String phoneNum = bundle.getString("phoneNum");
            tvPhone.setText(phoneNum);
        }
        if(!TextUtils.isEmpty((bundle.getString("email")))){
            email = bundle.getString("email");
            tvEmail.setText(email);
        }
    }

    @Override
    protected void initView() {
        super.initView();
        ButterKnife.bind(this);
        tvTitle.setText("设置");

       if(mRequest == null){
           mRequest = new MyAccountRequest();
           onNextListener = new SubscriberOnNextListener<AccountSettingRespond>() {
               @Override
               public void onNext(AccountSettingRespond subjects) {
                   AccountSettingRespond respond = (AccountSettingRespond)subjects;
                   phone = respond.getData().getTelphone();
                   email =(String) respond.getData().getEmail();
                   if(TextUtils.isEmpty(phone)){
                       tvPhone.equals("更换");
                   }else{
                       tvPhone.setText(phone);
                   }
                   if (TextUtils.isEmpty(email)){
                       tvEmail.setText("未绑定");
                   }else {
                       tvEmail.setText(email);
                   }
               }

               @Override
               public void onError(Throwable e) {

               }
           };
           fieldsMap = new HashMap<>();
           progressSubscriber = new ProgressSubscriber(onNextListener, mContext);
       }
        mRequest.getAccountSetting(progressSubscriber,fieldsMap);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null) {
            return;
        }
        if (requestCode == 1) {
            String new_phone = data.getExtras().getString("phone");
            tvPhone.setText("" + new_phone);
        } else if (requestCode == 2) {
            tvEmail.setText(data.getExtras().getString("email"));
        }else if(requestCode == 3){
            tvEmail.setText(data.getExtras().getString("email"));
        }

    }

    @OnClick({R.id.rl_back, R.id.rl_phone_num, R.id.rl_email, R.id.rl_password, R.id.tv_turn_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_phone_num: //手机号码
                Intent intent = new Intent(this,NewPhoneBinddingActivity.class);
                intent.putExtra("phone",phone);
                startActivityForResult(intent,1);
                break;
            case R.id.rl_email: //邮箱
                if(TextUtils.isEmpty(email)){
                    startActivityForResult(new Intent(this, EmailBinddingActivity.class), 2);
                }else{
                    Intent it = new Intent(this,EmailChangeActivity.class);
                    it.putExtra("phone",phone);
                    startActivityForResult(it,3);
                }
                break;
            case R.id.rl_password:  //登录密码
                Bundle bundle = new Bundle();
                bundle.putString("phone",tvPhone.getText().toString());
                openActivity(PwdResetByOldPwdActivity.class,bundle);
                break;
            case R.id.rl_back:
                finish();
                break;
            case R.id.tv_turn_back: //退出
                if (dialog == null) {
                    dialog = new BaseDialog.Builder().
                            layResuorce(R.layout.activity_account_setting_exit).
                            okViewClickEvent(R.id.tv_setting_ok).
                            cancelViewClickEvent(R.id.tv_setting_cancel).
                            gravityStyle(Gravity.CENTER).
                            animResource(R.style.dialog_animation_fade).
                            dialogWidth(-2).
                            build();
                    dialog.setOnDialogClickListener(new BaseDialog.OnDialogClickListener() {
                        @Override
                        public void onOkClick() {
                            dialog.dismiss();
                            SpConfig.getInstance().putString(Constants.SESSIONID_STRING, "");
                            SpConfig.getInstance().putBool(Constants.ISAUTH_BOOLEAN, false);
                            SpConfig.getInstance().putString(Constants.REALNAME_STRING, "");
                            SpConfig.getInstance().putString(Constants.TEPPHONE_STRING, "");
                            SpConfig.getInstance().putString(Constants.HEADERIMG_STRING, "");
                            SpConfig.getInstance().putString(Constants.RMAIL_STRING, "");
                            openActivity(LoginActivity.class);
                            finish();
                        }

                        @Override
                        public void onOk2Click() {

                        }
                    });
                }

                dialog.show(getSupportFragmentManager(), TAG);

                break;
        }
    }

}
